package com.neusoft.restful.controller;


import com.neusoft.restful.entity.Product;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/***
 * get 查询
 * post 添加
 * put 修改
 * delete 删除
 *
 */
@CrossOrigin
@RestController
public class RestFulController {

    //key/value
    @GetMapping("/queryall")
    public Map<String,Object> queryAll(Product product){
        System.out.println(product.getPname());
        List<Product> list = Arrays.asList(
                new Product(1,"辣条1","5.2"),
                new Product(1,"辣条2","5.2"),
                new Product(1,"辣条3","5.2"),
                new Product(1,"辣条4","5.2"),
                new Product(1,"辣条5","5.2")
        );
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","查询成功!!!");
        map.put("data",list);
        return map;
    }

    //json
    @PostMapping("/add_product")
    public Map<String,Object> add(@RequestBody Product product){

        System.out.println(product.getPname());
        List<Product> list = Arrays.asList(
                new Product(1,"辣条1","5.2"),
                new Product(1,"辣条2","5.2"),
                new Product(1,"辣条3","5.2"),
                new Product(1,"辣条4","5.2"),
                new Product(1,"辣条5","5.2")
        );

        //list.add(product);
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","添加成功!!!");
        map.put("data",list);
        map.put("product",product);
        return map;
    }

    @PutMapping("/modify_product")
    public Map<String,Object> update(@RequestBody Product product){

        System.out.println(product.getPname());
        List<Product> list = Arrays.asList(
                new Product(1,"辣条1","5.2"),
                new Product(1,"辣条2","5.2"),
                new Product(1,"辣条3","5.2"),
                new Product(1,"辣条4","5.2"),
                new Product(1,"辣条5","5.2")
        );

        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","修改成功!!!");
        map.put("data",list);
        return map;
    }

    @DeleteMapping("/del_product")
    public Map<String,Object> delete(@RequestBody Product product){
        System.out.println(product.getPid());
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","删除成功!!!");
        //map.put("data",list);
        return map;
    }

}
